package com.weaver.qfengx.config;

import com.weaver.qfengx.DaoUtils;
import com.weaver.qfengx.LogUtils;
import com.weaver.qfengx.StringUtils;
import org.apache.commons.compress.utils.Lists;

import java.util.List;
import java.util.Optional;

/**
 * 全局开发建模配置基类
 */
public class DevModeConfig {

    protected static List<PropConfig> configList = Lists.newArrayList();

    protected static String configTableName = "uf_devConfig";

    private static final LogUtils log = new LogUtils(DevModeConfig.class);

    static {
        try {
            loadConfig();
        } catch (Exception e) {
            e.printStackTrace();
            log.writeLog("配置表加载异常 => " + e.getMessage());
        }
    }

    public static void setConfigTableName(String tableName) {
        configTableName = tableName;
    }

    public static String getConfigTableName() {
        return configTableName;
    }

    public static void loadConfig() { // 加载配置信息
        configList = DaoUtils.queryTableToBean(PropConfig.class, String.format("select id, name, value, summary from %s", configTableName));
    }

    public static PropConfig getPropConfig(String name) {
        Optional<PropConfig> optionalPropConfig = configList.stream().filter(x -> StringUtils.equals(name, x.getName())).findFirst();
        return optionalPropConfig.orElse(null);
    }

    public static PropConfig getPropConfig(String name, String defaultVal) {
        Optional<PropConfig> optionalPropConfig = configList.stream().filter(x -> StringUtils.equals(name, x.getName())).findFirst();
        return optionalPropConfig.orElse(new PropConfig().setValue(defaultVal).setName(name));
    }

    public static List<PropConfig> getConfigList() {
        return configList;
    }

}
